;
(function () {
/**
 * Created by shen'jia on 15/6/9.
 */
'use strict';
angular.module('services.faultLocation.sysBusCount',[])
  .service('sysBusCountData',sysBusCountDataServer)//页面依赖数据
  .service('sysBusCountOption',sysBusCountOptionServer)//echarts
  .factory('sysBusCountprocessData',sysBusCountprocessDataServer)//响应时间处理
  .factory('sysBusCountServer',sysBusCountServer);//请求Url

function sysBusCountDataServer(COREDATA){
  var prov = angular.copy(COREDATA.PROVINCES); 
  prov.unshift({provinceName:'全部',provinceCode:'00'});
  this.selector = {
    system : COREDATA.SYSTEM,
    business : [{businessName:'签约'},{businessName:'解约'},{businessName:'缴费'}],
    displayS : [{disName:'总量'},{disName:'对比'}],
    channel : [{channelName:'天猫'},{channelName:'移动商城'},{channelName:'银行总对总'}],
    province : prov,
    time : new Date(new Date()-86400000*2)
  };
  this.sendData = {
    chan:[],
    systemName:'统一支付',
    businessName:'签约',
    displayMode:'总量',
    channelName:'天猫',
    provinceName:'全部',
    searchTime:''
  };
}
function sysBusCountOptionServer(){
  this.byTimeOp = {
    title : {text: '',x: 'center'},
    tooltip : {trigger: 'axis'},
    animation : false,
    grid : {width: 980, x2: 120, x: 70},
    legend : {
        show: true, orient: 'vertical',
        x: 'right', y: 50,
        data:['天猫']
    },
    
    xAxis : [{type: 'category', boundaryGap: true, splitLine: false, axisLine: {onZero: false}, data: ['0','1'], splitArea: true}],
    yAxis : [{name: '交易笔数', type: 'value'}],
    // series : [
    //   {name: '天猫', type: 'bar', smooth: true, data: [220, 182], symbolSize: 2, showAllSymbol: true},
    //   {name: '总对总', type: 'bar', smooth: true, data: [120, 132], symbolSize: 2, showAllSymbol: true},
    //   {name: '移动商城', type: 'bar', smooth: true, data: [320, 332], symbolSize: 2, showAllSymbol: true}
    // ]
    series : [
      // {name: '天猫', type: 'bar', smooth: true, data: [220, 182], symbolSize: 2, showAllSymbol: true}
    ]
  };
  this.byProvinceOp = {
    title : {text: '',x: 'center'},
    tooltip : {trigger: 'item'},
    animation : false,
    // grid : {width: 980, x2: 120, x: 70},
    series : [
        {name:'访问来源', type:'pie', radius : '55%', center: ['50%', '60%'],
            data:[
                {value:335, name:'直接访问'},
                {value:310, name:'邮件营销'},
                {value:234, name:'联盟广告'},
                {value:135, name:'视频广告'},
                {value:1548, name:'搜索引擎'}
            ]
        }
    ]
  };
  this.successRateOp = {
    title : {text: '',x: 'center'},
    tooltip : {trigger: 'axis'},
    animation : false,
    grid : {width: 980, x2: 120, x: 70},
    dataZoom: {show: true,  start : 0,end :100},
    legend : {
        show: true, orient: 'vertical',
        x: 'right', y: 50,
        data:[]
    },
    xAxis : [{type: 'category', boundaryGap: true, splitLine: false, axisLine: {onZero: false}, data: [], splitArea: true}],
    yAxis : [{name: '百分比', type: 'value' , axisLabel: {formatter: '{value}%'}}],
    series : []
  };
  this.thirtyOp = {
    title : {text: '',x: 'center'},
    tooltip : {trigger: 'axis'},
    animation : false,
    grid : {width: 980, x2: 120, x: 70},
    dataZoom: {show: true,  start : 0,end :100},
    legend : {
        show: true, orient: 'vertical',
        x: 'right', y: 50,
        data:[]
    },
    xAxis : [{type: 'category', boundaryGap: true, splitLine: false, axisLine: {onZero: false}, data: [], splitArea: true}],
    yAxis : [{name: '交易笔数', type: 'value'}],
    series : []
  };
}
function sysBusCountprocessDataServer(sysBusCountData){
  return{
    byTimePro:process1,
    byProvincePro:process2,
    successRatePro:process3,
    thirtyPro:process4,
  };
  function process1(chart,option,title,data){
    chart.clear();
    var xBar = [];
    var yList = [];
    var lege = [];
    if(data[0].data1){
      for(var ii =0;ii<data[0].data1.length;ii++){
        yList[ii] = [];
      }
    }else{
      yList[0] = [];
    }
    for(var i=0;i<data.length;i++){
      xBar[i] = data[i].time;
      if(data[0].data1){
        for(var j =0;j<data[0].data1.length;j++){
          yList[j].push(data[i].data1[j].dealNum);
        }
      }else{
        yList[0].push(data[i].channel[0].dealNum);
      }
    }
    option.title.text =  title.businessName + '业务成交量分时刻统计_' + title.searchTime;
    chart.setOption(option);
    chart.setOption({ xAxis :[{data:xBar}]  });
    var yArray = [];
    for(var k=0;k<yList.length;k++){
      yArray[k] = {name: '汇总', type: 'bar', smooth: true, data: [220, 182], symbolSize: 2, showAllSymbol: true};
      if(data[0].data1){
        lege.push(data[0].data1[k].channelName);
        yArray[k].name = data[0].data1[k].channelName;
      }else{
        lege.push('汇总');
        yArray[k].name = '汇总'
      }
      yArray[k].data = yList[k];
    }
    chart.setOption({ legend: {data:lege} });
    chart.setOption({ series :yArray  });
  }

  function process2(chart,option,title,data){
    if(chart.length){
      for(var k=0;k<chart.length;k++){
        chart[k].clear();
        option[k].title.text = data[k].channelName +sysBusCountData.sendData.businessName + '业务成交量分省占比统计';
        chart[k].setOption(option[k]);
        var arry = [];
        for(var i=0;i<data[k].data1.length;i++){
          arry[i] = {};
          arry[i].value = data[k].data1[i].provincePercentage;
          arry[i].name = data[k].data1[i].provinceName;
        }
        chart[k].setOption({series:[{data:arry}]});
      }
    }else{
      chart.clear();
      if(sysBusCountData.sendData.displayMode === '总量'){
        option.title.text = sysBusCountData.sendData.systemName + sysBusCountData.sendData.businessName + '业务成交量分省占比统计';
      }
      if(sysBusCountData.sendData.displayMode === '对比'){
        option.title.text = data[0].channelName + sysBusCountData.sendData.businessName + '业务成交量分省占比统计';
      }
      chart.setOption(option);
      var arry = [];
      for(var i=0;i<data[0].data1.length;i++){
        arry[i] = {};
        arry[i].value = data[0].data1[i].provincePercentage;
        arry[i].name = data[0].data1[i].provinceName;
      }
      chart.setOption({series:[{data:arry}]});
    }
  }

  function process3(chart,option,title,data){
    chart.clear();
    var xBar = [];
    var yList = [];
    var lege = [];
    if(data[1]){
      for(var ii =0;ii<data.length;ii++){
        yList[ii] = [];
      }
    }else{
      yList[0] = [];
    }
    for(var i=0;i<data[0].data1.length;i++){
      xBar[i] = data[0].data1[i].provinceName;
      if(data[1]){
        for(var j =0;j<data.length;j++){
          yList[j].push(data[j].data1[i].provinceSuccessRate);
        }
      }else{
        yList[0].push(data[0].data1[i].provinceSuccessRate);
      }
    }
    option.title.text = sysBusCountData.sendData.businessName +'业务成交率';
    chart.setOption(option);
    chart.setOption({ xAxis :[{data:xBar}]  });
    var yArray = [];
    for(var k=0;k<yList.length;k++){
      yArray[k] = { type: 'bar', smooth: true, data: [220, 182], symbolSize: 2, showAllSymbol: true};
      if(data[0].businessName){
        lege.push('汇总');
        yArray[k].name = '汇总';
      }else{
        lege.push(data[k].channelName);
        yArray[k].name = data[k].channelName;
      }
      yArray[k].data = yList[k];
    }
    chart.setOption({ legend: {data:lege} });
    chart.setOption({ series :yArray  });
    chart.setOption(option);

  }
  function process4(chart,option,title,data){
    chart.clear();
    var xBar = [];
    var yList = [];
    var lege = [];
    if(data[0].data1){
      for(var ii =0;ii<data[0].data1.length;ii++){
        yList[ii] = [];
      }
    }else{
      yList[0] = [];
    }
    for(var i=0;i<data.length;i++){
      xBar[i] = data[i].time;
      if(data[0].data1){
        for(var j =0;j<data[i].data1.length;j++){
          yList[j].push(data[i].data1[j].dealNum);
        }
      }else{
        yList[0].push(data[i].channel[0].dealNum);
      }
    }
    option.title.text = '近30天'+ sysBusCountData.sendData.businessName+'业务成交量趋势统计';
    chart.setOption(option);
    chart.setOption({ xAxis :[{data:xBar}]  });
    var yArray = [];
    for(var k=0;k<yList.length;k++){
      yArray[k] = {name: '汇总', type: 'bar', smooth: true, data: [220, 182], symbolSize: 2, showAllSymbol: true};
      if(data[0].data1){
        if(data[0].data1[k].channelName === ''){
          lege.push('天猫');
          yArray[k].name = '天猫';
        }
        if(data[0].data1[k].channelName !== ''){
          lege.push(data[0].data1[k].channelName);
          yArray[k].name = data[0].data1[k].channelName;
        }
        
      }else{
        lege.push('汇总');
        yArray[k].name = '汇总'
      }
      yArray[k].data = yList[k];
    }
    chart.setOption({ legend: {data:lege} });
    chart.setOption({ series :yArray  });
    chart.setOption(option); 
  }
}
function sysBusCountServer(Restangular,CoreService,sysBusCountprocessData,sysBusCountData){
  function duParam(type){
    if(type ==='byTime'){
      var params = angular.copy(sysBusCountData.sendData);
      var channelN = '';
      params.searchTime = CoreService.dateFormat(sysBusCountData.selector.time,'yyyy-MM-dd');
      if(sysBusCountData.sendData.displayMode === '总量'){
        params.channelName ='';
        params.chan = undefined;
      }else{
        if(params.chan.length>0){
          for(var i =0;i<params.chan.length;i++){
            channelN += params.chan[i].channelName + ',';
          }
          params.channelName = channelN.substr(0,channelN.length-1);
          params.chan = undefined;
        }else{
          params.channelName = '天猫';
          params.chan = undefined;
        }
      }
      return params;
    }
  };
  return{
    byTime:function (chart,option,title){
      var params = duParam('byTime');
      Restangular.all('sas/OMdisplay/business/byTime').post(params).then(function(res){
        if(res.state==='10000'){
          sysBusCountprocessData.byTimePro(chart,option,params,res.data);
        }
      });
    },
    byProvince:function (chart,option,title){
      var params = duParam('byTime'); 
      // params.provinceName = undefined;      
      Restangular.all('sas/OMdisplay/business/byProvince').post(params).then(function(res){
        sysBusCountprocessData.byProvincePro(chart,option,title,res.data);
      });
    },
    successRate:function (chart,option,title){
      var params = duParam('byTime');     
      Restangular.all('sas/OMdisplay/business/successRate').post(params).then(function(res){
        sysBusCountprocessData.successRatePro(chart,option,title,res.data);
      });
    },
    thirty:function (chart,option,title){
      var params = duParam('byTime'); 
      Restangular.all('sas/OMdisplay/business/thirty').post(params).then(function(res){
        sysBusCountprocessData.thirtyPro(chart,option,title,res.data);
      });
    }
  };
  
}
})();